from PySide6.QtCore import Qt, Signal
from qfluentwidgets import SettingCard, FluentIconBase,LineEdit
from app.common.config import cfg
class LineEditSettingCard(SettingCard):
    """ 文本输入设置卡片 """
    
    valueChanged = Signal(str)

    def __init__(self, icon: FluentIconBase, title: str, content: str, 
                 configItem=None, parent=None):
        super().__init__(icon, title, content, parent)
        self.configItem = configItem
        
        # 创建文本输入框
        self.lineEdit = LineEdit(self)
        if self.configItem:
            try:
                self.lineEdit.setText(self.configItem.value)
            except:
                pass
        self.lineEdit.setMinimumWidth(200)
  
        
        # 将文本输入框添加到右侧
        self.hBoxLayout.addWidget(self.lineEdit, 0, Qt.AlignRight)
        self.lineEdit.textChanged.connect(self._onValueChanged)

    def _onValueChanged(self, text: str):
        """ 值改变时的处理 """
        # 将\\n转换为unicode转义,其他字符保持不变
        text_with_escaped_newlines = text.replace('\\n', '\\\\n').replace('\\r', '\\\\r')
        if self.configItem:
            self.configItem.value = text_with_escaped_newlines
            cfg.save()
        self.valueChanged.emit(text_with_escaped_newlines)

    def getValue(self):
        """ 获取当前值 """
        return self.lineEdit.text()

    def setValue(self, value: str):
        """ 设置值 """
        self.lineEdit.setText(value) 